package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTabbedPane;

import util.DBUtil;

import model.JobTitle;
import dao.JobTitleDAO;

public class JobTitleDAOIml implements JobTitleDAO {

	DBUtil dbu = new DBUtil();
	Connection conn = null;
	PreparedStatement stat = null;
	ResultSet rs = null;
	boolean result = false;

	public List<JobTitle> getAll() {
		List<JobTitle> jobTitles = new ArrayList<JobTitle>();
		JobTitle jt = new JobTitle();

		try {
			conn = dbu.getConnection();
			stat = conn.prepareStatement("select * from jobtitle;");
			rs = stat.executeQuery();

			while (rs.next()) {
				jt.setId(rs.getInt("ID"));
				jt.setName(rs.getString("NAME"));
				jobTitles.add(jt);
			}

			return jobTitles;
		} catch (Exception e) {
		} finally {
			dbu.closeConnection(conn, stat, rs);
		}
		return null;
	}

	public JobTitle getById(int id) {

		JobTitle jt = null;

		try {
			conn = dbu.getConnection();
			stat = conn.prepareStatement("select * from jobtitle where id=?");
			stat.setInt(1, id);
			rs = stat.executeQuery();

			if (rs.next()) {
				jt=new JobTitle();
				jt.setId(rs.getInt("ID"));
				jt.setName(rs.getString("NAME"));
				return jt;
			}
		} catch (Exception e) {
		}finally{
			dbu.closeConnection(conn, stat, rs);
		}

		return jt;
	}

	public JobTitle getByName(String name) {
		JobTitle jt = null;

		try {
			conn = dbu.getConnection();
			stat = conn.prepareStatement("select * from jobtitle where name=?;");
			stat.setString(1, name);
			rs = stat.executeQuery();

			if (rs.next()) {
				jt=new JobTitle();
				
				jt.setId(rs.getInt("ID"));
				jt.setName(rs.getString("NAME"));
				
				return jt;
			}
		} catch (Exception e) {
		}finally{
			dbu.closeConnection(conn, stat, rs);
		}

		return jt;
	}
}
